<?php
namespace Admin\Controller;

class HolidayController extends BaseController {

    public function index() {

        $Holiday = D ('Holiday');
        $map['teacher_id'] = $this->admin['id'];

        // 分页处理
        $limit = \Api\Libs\PageUtil::getLimit(10);
        $page = \Api\Libs\PageUtil::getPageHtml($Holiday->getCountByWhere($map),10);
        $holidayList = $Holiday->getHolidayByWhere($map,$limit);

        $Student = D ('Student');
        foreach ($holidayList as $key=>$val) {
            $holidayList[$key]['sname'] = $Student->getStudentNameById($val['student_id']);
        }

        $this->assign('holidayList',$holidayList);
        $this->assign('page',$page);
        $this->display();
    }

    /**
     * 处理请假审核
     */
    public function handler () {

        $this->isPost();

        $data['id'] = I ('post.id','');
        $data['status'] = I ('post.status','');
        $data['response'] = I ('post.response','');

        if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        $Holiday = D ('Holiday');
        $flag = $Holiday->handlerJt($data);
        if ($flag !== '000000') {
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }

        // 推送相关学生推送请假结果信息
        $student_id = $Holiday->getStudentId($data['id']);
        $JPush = new \Api\Libs\JPush();
        $receiver = D ('JpushMapping')->getReceiverBySid($student_id);
        $msg = array(
            'title' => $data['status'] === 1?'请假成功':'请假失败',
            'content' => $data['response'],
        );

        // 极光推送，不处理推送结果
        $result = json_decode($JPush->pushNotification($msg,$receiver),true);
        $this->out('RESULT_OK','',__CONTROLLER__);

    }

}